package com.lou.springboot.service.impl;

import com.lou.springboot.common.PageResult;
import com.lou.springboot.dao.BlogCommentDao;
import com.lou.springboot.entity.BlogComment;
import com.lou.springboot.service.CommentService;
import com.lou.springboot.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guaguaerhao on 2020/7/17 23:02
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private BlogCommentDao commentDao;

    @Override
    public PageResult getCommentsPage(PageUtil pageUtil) {
        List<BlogComment> list = commentDao.findCommentList(pageUtil);
        int total = commentDao.getTotalComments();
        return new PageResult(list,total,pageUtil.getLimit(),pageUtil.getPage());
    }

    @Override
    public Boolean addComment(BlogComment blogComment) {
        return commentDao.insertBlogComment(blogComment) > 0;
    }

    @Override
    public PageResult getCommentPageByBlogIdAndPageNum(Long blogId, Integer commentPage) {
        Map map = new HashMap();
        map.put("page",commentPage);
        map.put("limit",9);
        map.put("blogId",blogId);
        map.put("commentStatus",1);
        PageUtil pageUtil = new PageUtil(map);
        List<BlogComment> blogComments = commentDao.getCommentPage(pageUtil);
        if (!CollectionUtils.isEmpty(blogComments)) {
            int total = commentDao.getTotalCommentsByBlogId(map);
            return new PageResult(blogComments,total,pageUtil.getLimit(),pageUtil.getPage());
        }
        return null;
    }

    @Override
    public Boolean done(Integer[] commentIds) {
        return commentDao.done(commentIds) > 0;
    }

    @Override
    public Boolean reply(Long commentId, String replyBody) {
        BlogComment blogComment = commentDao.selectByPrimaryKey(commentId);
        if (blogComment != null && blogComment.getCommentStatus().intValue() == 1) {
            blogComment.setReplyBody(replyBody);
            blogComment.setReplyCreateTime(new Date());
            return commentDao.updateByPrimaryKeySelective(blogComment) > 0;
        }
        return false;
    }

    @Override
    public Boolean deleteBatch(Integer[] ids) {
        return commentDao.deleteBatch(ids) > 0;
    }
}
